package com.uls.web.controller.found;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.common.core.domain.entity.SysUser;
import com.uls.found.domain.EduClaimRecord;
import com.uls.system.service.ISysUserService;
import io.swagger.models.auth.In;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.found.domain.EduReportRecordLoss;
import com.uls.found.service.IEduReportRecordLossService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 报失记录Controller
 * 
 * @author ruoyi
 * @date 2024-10-30
 */
@RestController
@RequestMapping("/found/loss")
public class EduReportRecordLossController extends BaseController
{
    @Autowired
    private IEduReportRecordLossService eduReportRecordLossService;

    @Autowired
    private ISysUserService iSysUserService;
    /**
     * 查询报失记录列表
     */
    @PreAuthorize("@ss.hasPermi('found:loss:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduReportRecordLoss eduReportRecordLoss)
    {
        startPage();
        List<EduReportRecordLoss> list = eduReportRecordLossService.selectEduReportRecordLossList(eduReportRecordLoss);

        for (EduReportRecordLoss eduReportRecordLoss1:list){

            SysUser sysUser=iSysUserService.selectUserById(eduReportRecordLoss1.getStudentId());

             eduReportRecordLoss1.setUserName(sysUser.getUserName());
        }
        return getDataTable(list);
    }

    /**
     * 导出报失记录列表
     */
    @PreAuthorize("@ss.hasPermi('found:loss:export')")
    @Log(title = "报失记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduReportRecordLoss eduReportRecordLoss)
    {
        List<EduReportRecordLoss> list = eduReportRecordLossService.selectEduReportRecordLossList(eduReportRecordLoss);
        ExcelUtil<EduReportRecordLoss> util = new ExcelUtil<EduReportRecordLoss>(EduReportRecordLoss.class);
        util.exportExcel(response, list, "报失记录数据");
    }

    /**
     * 获取报失记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('found:loss:query')")
    @GetMapping(value = "/{lostId}")
    public AjaxResult getInfo(@PathVariable("lostId") Integer lostId)
    {
        return success(eduReportRecordLossService.selectEduReportRecordLossByLostId(lostId));
    }

    /**
     * 新增报失记录
     */
    @PreAuthorize("@ss.hasPermi('found:loss:add')")
    @Log(title = "报失记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduReportRecordLoss eduReportRecordLoss)
    {
        eduReportRecordLoss.setCreateBy(getUsername().toString());
        eduReportRecordLoss.setCreateTime(new Date());

        eduReportRecordLoss.setStudentId(getUserId());
        return toAjax(eduReportRecordLossService.insertEduReportRecordLoss(eduReportRecordLoss));
    }

    /**
     * 修改报失记录
     */
    @PreAuthorize("@ss.hasPermi('found:loss:edit')")
    @Log(title = "报失记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduReportRecordLoss eduReportRecordLoss)
    {
        eduReportRecordLoss.setUpdateBy(getUsername().toString());
        eduReportRecordLoss.setUpdateTime(new Date());

        eduReportRecordLoss.setStudentId(getUserId());
        return toAjax(eduReportRecordLossService.updateEduReportRecordLoss(eduReportRecordLoss));
    }

    /**
     * 删除报失记录
     */
    @PreAuthorize("@ss.hasPermi('found:loss:remove')")
    @Log(title = "报失记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lostIds}")
    public AjaxResult remove(@PathVariable Integer[] lostIds)
    {

        for (Integer lostId:lostIds){

         EduReportRecordLoss eduReportRecordLoss= eduReportRecordLossService.selectEduReportRecordLossByLostId(lostId);

            if (eduReportRecordLoss != null) {

                eduReportRecordLoss.setDeleteBy(getUsername().toString());

                eduReportRecordLoss.setDeleteTime(new Date());

                eduReportRecordLoss.setDelFlag(1);

                eduReportRecordLossService.updateEduReportRecordLoss(eduReportRecordLoss);

            }
        }
        return AjaxResult.success();
    }


    //下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<EduReportRecordLoss> util = new ExcelUtil<EduReportRecordLoss>(EduReportRecordLoss.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @Log(title = "报失管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('found:loss:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<EduReportRecordLoss> util = new ExcelUtil<EduReportRecordLoss>(EduReportRecordLoss.class);
        List<EduReportRecordLoss> eduReportRecordLossList = util.importExcel(file.getInputStream());
        String message = eduReportRecordLossService.importUser(eduReportRecordLossList, updateSupport);
        return success(message);
    }
}
